package com.ts.core.controller.android;


import com.ts.core.Utils.RandomStringGenerator;
import com.ts.core.entity.AndroidVo.AnArticleVo;
import com.ts.core.entity.AndroidVo.AnPostArticleVo;
import com.ts.core.entity.LxbArticle;
import com.ts.core.entity.LxbBlogger;
import com.ts.core.entity.LxbUser;
import com.ts.core.entity.Vo.IdentityVo;
import com.ts.core.service.LxbArticleService;
import com.ts.core.service.LxbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-04-01
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/android/core/lxbuser")
public class LxbUserController {

    @Resource
    LxbUserService lxbUserService;
    @Resource
    LxbArticleService lxbArticleService;
    @Resource
    private RedisTemplate redisTemplate;



    
    //新建Android手机用户
    //设置默认昵称、默认头像、默认朋友数
    //认证之后成为博主
    //成为博主之后可以发布自己的作品
    @ApiOperation("创建手机用户")
    @GetMapping("saveuser/{account}/{password}")
    public Map<String,Object> createUser(
            @PathVariable String account,
            @PathVariable String password
    ){
        //根据账号密码注册新用户
        lxbUserService.CreateUser(account,password);
        HashMap<String, Object> Map = new HashMap<>();
        Map.put("code",0);
        Map.put("message","成功");
        return Map;



    }
    
    @ApiOperation("根据账号密码查询出用户")
    @GetMapping("getuser/{account}/{password}")
    public Map<String,Object> GetUser(
            @PathVariable String account,
            @PathVariable String password
    ){
       LxbUser lxbUser= lxbUserService.GetUser(account,password);
        System.out.println("============================================");
        System.out.println(lxbUser);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",lxbUser);
        return map;

    }
    @ApiOperation("获取全部的帖子")
    @GetMapping("getAllArticle")
    public Map<String,Object> getAllArtcle(){
       List<AnArticleVo> anArticleVoList=lxbArticleService.getAllArticle();
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",anArticleVoList);
        return map;

    }

    //上传视频、图片到阿里云后返回要存储的信息 如果是是视频要包含视频的封面
    
    //在手机端发帖子 在发布视频和图片后
    @ApiOperation("发帖子")
    @PostMapping("CreateArticle")
    public Map<String,Object> CreateArticle(
            @RequestBody AnPostArticleVo anArticleVo)
    {
        lxbArticleService.CreateArticleAndVideo(anArticleVo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","成功");

        return map;


    }

    @ApiOperation("验证手机号是否已经被注册")
    @GetMapping("getByMobile/{mobile}")
    public Map<String,Object> getByMobile(
            @PathVariable String mobile
    ){
       boolean b= lxbUserService.checkMobile(mobile);
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","成功");
        map.put("data",b);
        return map;





    }

    @ApiOperation("验证码的验证")
    @GetMapping("Verify/{code}/{mobile}")
    public Map<String,Object> VerifyByCode(
            @PathVariable String code,
            @PathVariable String mobile
    ){
        HashMap<String, Object> map = new HashMap<>();

        String  verify = (String) redisTemplate.opsForValue().get("lxb:sms:code:"+mobile);
        if(code.equals(verify))
        {
            map.put("code","0");
            map.put("message","验证成功");
        }
        else
        {
            map.put("code","404");
            map.put("message","验证码输入错误");

        }
        System.out.println(map);
        return map;

    }

    @ApiOperation("用户认证为博主")
    @PostMapping("identity")
    public Map<String,Object> VerifyByCode(
            @RequestBody IdentityVo identityVo
            ){
        HashMap<String, Object> map = new HashMap<>();
        lxbUserService.identityUser(identityVo);
        map.put("code",0);
        map.put("message","认证成功");
        return map;


    }
    @ApiOperation("根据博主的id获取到其用户账号")
    @GetMapping("getUserAccount/{bloggerId}")
    public Map<String,Object>  getAccountByBloggerId(
            @PathVariable String bloggerId
    ){
        HashMap<String, Object> map = new HashMap<>();
        String account=lxbUserService.getAccountByBloggerId(bloggerId);
        map.put("code",0);
        map.put("account",account);
        map.put("message","认证成功");
        return map;


    }






}

